import {loginData} from "../api/Login"
import { router } from "@kit.ArkUI"

export const UserStoreKey="hc-user"
class Auth{
  initUser(){
    //登录后第一次存储持久化状态
    PersistentStorage.persistProp<loginData>(UserStoreKey,{}as loginData)
  }
  getUser(){
    return AppStorage.get<loginData>(UserStoreKey)||{} as loginData

  }
  setUser(user:loginData){
    AppStorage.setOrCreate<loginData>(UserStoreKey,user)
  }
  checkAuth(options:router.RouterOptions|Function){
    const user=this.getUser()
    if(user.token){
      if(typeof options==='function'){
        options()
      }else {
        router.pushUrl(options)
      }
    }else{
      if(typeof  options === 'function'){
        router.pushUrl({
          url:"pages/Login"
        })
      }else{
        const params=options.params as Record<string,string>||{}
        params.return_url=options.url
        router.pushUrl({
          url:"pages/Login",
          params
        })
      }
    }
  }
}
export const auth=new Auth()